package service;

import bean.Cyclingrecord;
import dao.CyclingrecordDAO;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import util.JsonWrapper;
import util.JsonWrapperBuilder;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Optional;

/**
 * @author liu
 */
public class CyclingrecordService {

    private CyclingrecordDAO cyclingrecordDAO = new CyclingrecordDAO();

    public JSONObject cyclingrecordsPagination(int userId, int start, int pagesize) {
        List<Cyclingrecord> cyclingrecordList = cyclingrecordDAO.getPage(userId, (start - 1) * pagesize, pagesize);
        JSONArray cyclingRecordArray = new JSONArray();
        for (int i = 0; i < cyclingrecordList.size(); i++) {
            cyclingRecordArray.add(getOneCyclingRecordJson(cyclingrecordList.get(i)));
        }
        return new JsonWrapperBuilder()
            .setStatus(200)
            .setMsg("success")
            .setDataJsonArray("allBrand", cyclingRecordArray)
            .build();
    }

    private JSONObject getOneCyclingRecordJson(Cyclingrecord cyclingrecord) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        JsonWrapper value = new JsonWrapper();
        value.setEntry("cyclingRecordId", cyclingrecord.getCyclingrecordid());
        value.setEntry("bicycleId", cyclingrecord.getBicycleid());
        value.setEntry("userId", cyclingrecord.getUserid());
        value.setEntry("startLocX", cyclingrecord.getStartlocx());
        value.setEntry("startLocY", cyclingrecord.getStartlocy());
        value.setEntry("startTime", sdf.format(cyclingrecord.getStarttime()));
        value.setEntry("endTime", sdf.format(cyclingrecord.getEndtime()));
        value.setEntry("endLocX", cyclingrecord.getEndlocx());
        value.setEntry("endLocY", cyclingrecord.getEndlocy());
        return value.getNoData();
    }

    public JSONObject cyclingrecordTotal() {
        int total = cyclingrecordDAO.getTotal();
        return new JsonWrapperBuilder()
            .setStatus(200)
            .setMsg("success")
            .setDataEntry("total", total).build();
    }

    public JSONObject findByCId(String cyclingRecordId) {
        try {
            Optional<Cyclingrecord> ocr = cyclingrecordDAO.findByCId(cyclingRecordId);
            if (ocr.isPresent()) {
                Cyclingrecord cr = ocr.get();
                return JsonWrapper.builder().setStatus(200).setMsg("success")
                    .setDataEntry("bid", cr.getBicycleid())
                    .setDataEntry("startLocX", cr.getStartlocx())
                    .setDataEntry("startLocY", cr.getStartlocy())
                    .setDataEntry("endLocX", cr.getEndlocx())
                    .setDataEntry("endLocY", cr.getEndlocy())
                    .build();
            } else {
                return JsonWrapper.builder().setStatus(404).setMsg("not found").build();
            }
        } catch (SQLException e) {
            e.printStackTrace();
            return JsonWrapper.builder().setStatus(500).setMsg("Internal error").build();
        }
    }
}
